﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Home.Haushalt.Lib
{


    /// <summary>
    /// 
    /// </summary>
    public class HCredential
    {
        /// <summary></summary>
        private const string MASTERIDENT = "hlajshf%H$$jh443'úpöalsd9fFJS^la^tkû";

        /// <summary></summary>
        public HCryptography CurrentCry;

        /// <summary></summary>
        public bool PassAssigned { get { return (DAO.Settings.Selected.MasterIdent.Trim().Length != 0); } }

        /// <summary></summary>
        public bool AccessAllowed { get { return (CurrentCry != null); } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pass"></param>
        /// <returns>Successed</returns>
        internal bool Login(string pass)
        {
            bool logged = false;

            HCryptography currentKeyCry = new HCryptography(pass);

            try
            {
                logged = currentKeyCry.Decrypt64(DAO.Settings.Selected.MasterIdent).Equals(MASTERIDENT);
                CurrentCry = currentKeyCry;
            }
            catch { }

            return logged;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newPass"></param>
        /// <returns></returns>
        public bool Update(string newPass)
        {
            HCryptography newKeyCry = new HCryptography(newPass);

            bool result = DAO.CredentialUpdate(newKeyCry);

            if (result)
            {
                DAO.Settings.Selected.MasterIdent = newKeyCry.Encrypt64(MASTERIDENT);

                result = DAO.Login(newPass);
            }

            return result;
        }

    }
}
